<?php

/*
 * 2021/7/16
 * 20:06
 * User: 2650332702@qq.com
 */

namespace App\Api\Repositories;


use App\Api\Models\CmcPromotionPintuan;
use App\Api\Models\CmcPromotionPintuanGroup;
use App\Api\Models\CmcPromotionPintuanOrder;
use App\Api\Models\Goods;
use App\Api\Models\ShippingAddress;
use App\Api\Services\JsonService;
use Illuminate\Http\Request;

class GroupingRepositorie
{
    //获得参与拼团商品
    static public function haveExistence(int $uid)
    {
        $allow_field = [
            'goods_id',
            'pintuan_id',
            'head_id',
            'pintuan_num',
            'pintuan_count',
            'end_time'
        ];
        //判断是否存在砍价行为
        $data_pintuan = CmcPromotionPintuanGroup::judgment($uid,$allow_field,2);
        if(empty($data_pintuan)){
            return false;
        }else{
            $allow_field = [
                'goods_id',
                'pintuan_id',
                'pintuan_image',
                'group_num',
                'pintuan_num',
                'pintuan_time'
            ];
            //根据商品ID查询砍价商品
            foreach ($data_pintuan as &$item){
                $item['goods'] = CmcPromotionPintuan::oncePintuan($item['pintuan_id'],$allow_field);
            }
            return $data_pintuan;
        }
    }

    //拼团列表展示
    static public function groupList(string $time)
    {
        $allow_field = [
            'pintuan_id',
            'goods_id',
            'pintuan_name',
            'group_num',
            'pintuan_image',
            'pintuan_price'
        ];
        $groupList = CmcPromotionPintuan::inTime($time,$allow_field);
        if(empty($groupList)){
            return false;
        }else{
            return $groupList;
        }
    }
    //拼团商品详细信息
    static public function groupDetail(int $pid)
    {
        $allow_field = [
            'pintuan_num',
            'pintuan_time',
            'pintuan_price',
            'order_num',
            'group_num',
            'goods_id'
        ];
        $data = [
            'goods_id',
            'goods_introduction',
            'goods_name',
            'goods_image',
            'price',
            'goods_content'
        ];
        //获取拼团商品
        $pintuan = CmcPromotionPintuan::oncePintuan($pid,$allow_field);
        //获得商品的详情
        $goods = Goods::OnceGood($pintuan['goods_id'],$data);
        return array_merge($pintuan,$goods);
    }

    //获取拼团组
    static public function getAllGroup($pid,$allow_field)
    {
        return CmcPromotionPintuanGroup::getPidAll($pid,$allow_field);
    }
    //获取拼团活动ID
    static public function joinPintuan($uid,$allow_field,$addr_id,$pid)
    {
        //获取活动详情
        $pintuan = CmcPromotionPintuan::getGoodsDetail($pid,$allow_field);
        $pintuan['head_id'] = $uid;
        $pintuan['status']=0;
        $pintuan['addr_id']=$addr_id;
        return CmcPromotionPintuanGroup::getPintuanData($pintuan);
    }
    //查询用户是否已经拼团
    static public function is_pintuan($uid,$pid,$status = 0)
    {
        return CmcPromotionPintuanGroup::isPintuan($uid,$pid,$status);
    }

    //查询用户地址
    static public function getAddr($addr_id)
    {
        return  ShippingAddress::getAddr($addr_id);
    }

    //查询拼团订单列表
    static public function pintuanOrder($uid)
    {
        return CmcPromotionPintuanOrder::getOrder($uid);
    }
    //获得拼团商品信息
    static public function getPintuanInfo($pintuan_id)
    {
        //根据拼团ID查询商品ID
        $goods = CmcPromotionPintuan::oncePintuan($pintuan_id,['goods_id','pintuan_name','pintuan_price']);
        $goods['goods_price'] = Goods::OnceGood($goods['goods_id'],'price')['price'];
        return $goods;
    }

}
